package com.hly.musicbackground.model.movies.service.impl;

import com.hly.musicbackground.Final.MoviesFromFinal;
import com.hly.musicbackground.domain.Movies;
import com.hly.musicbackground.domain.MoviesEpisodes;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MoviesTencentVideo {

    public List<Movies> searchDomToObject(Document document) {
        ArrayList<Movies> movies = new ArrayList<>();

        Elements infos = document.select("._infos");

        infos.stream().forEach(n -> {
            if (n.select(".figure.result_figure")
                    .attr("href").indexOf("search_redirect") == -1){

                Elements select = n.select(".result_info .content");
                int i = 1;
                if (select.size() < 4){
                    i -= 1;
                }

                Movies mov = new Movies();

                // 获取影视封面
                mov.setImage(n.select("a.figure.result_figure img.figure_pic").attr("src"));
                if (mov.getImage().indexOf("//") == 0 && mov.getImage().indexOf("http://") == -1){
                    mov.setImage(mov.getImage().replace("//", "http://"));
                }

                // 设置来源
                mov.setResourcesFrom(MoviesFromFinal.Tencent);

                // 获取默认链接
                mov.setUrl(n.select("a.figure.result_figure").attr("href"));

                // 获取影视名称
                mov.setMoviesName(n.select(".result_title em").text());


                // 获取影视类型
                mov.setType(n.select("h2 .type").text());

                // 获取其他信息
                mov.setSub(n.select("h2 .sub").text());

                // 获取英文名
                mov.setEnglishName(select.get(i).text());

                // 获取导演
                mov.setDirect(select.get(i).text());

                // 获取主演
                mov.setActLeadingRole(select.get(i).text());

                // 获取简介
                mov.setDescription(n.select(".info_item .desc_text").text());

                movies.add(mov);
            }
        });
//        System.out.println(infos);

        return movies;
    }


    public List<MoviesEpisodes> getMoviesEpisodes(Document allDoc, Movies movies) {
        ArrayList<MoviesEpisodes> list = new ArrayList<>();

        Elements select = allDoc.select(".b-sticky__scroller .episode-list-rect__item");
        int i = 1;
        select.stream().forEach(n -> {
            MoviesEpisodes episodes = new MoviesEpisodes();

            episodes.setMoviesId(movies.getMoviesId());

            episodes.setTitle(n.select("span").text());

            episodes.setNumberEpisodes(i);

            episodes.setUrl(n.select(".episode-item").attr("data-vid"));

            list.add(episodes);
        });

        return list;
    }
}
